<?php

namespace app\api\controller\shop;

use app\admin\model\Devicefill;
use app\admin\model\Deviceslot;
use app\admin\model\Filldetail;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Fill extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function fillList()
    {
        $user = $this->auth->getUser();
        $page = $this->request->param('page');
        $device_id = $this->request->param('device_id');
        $start = $this->request->param('start');
        $end = $this->request->param('end');

        $user_id = $this->request->param('user_id');

        $where = array();

        if($user_id != ""){
            $where['user_id'] = $user_id;
        }
        if($start != ''){
            $start = strtotime($start." 00:00");
            $end = strtotime($end." 23:59");
            $where['create_time'] = array("between",array($start,$end));
        }
        if($device_id != ''){
            $where['Devicefill.device_id'] = $device_id;
        }

        $staffModel = new \app\admin\model\Staff();
        $isStaff = -1;
        $staff = $staffModel->where('user_id',$user['id'])->find();
        if($staff){
            $isStaff= 1;
        }

        if($device_id == '' && $user_id == '' && $isStaff == -1){

            $deviceModel = new \app\admin\model\Device();
            $deviceList = $deviceModel->where('agent_id',$user['id'])->select();
            $ids = array();
            foreach ($deviceList as $key=>$val){
                $ids[] = $val['device_id'];
            }
            $where['Devicefill.device_id'] = ['in',$ids];

        }

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $deviceFillModel = new Devicefill();
        $list = $deviceFillModel->with(['user','device'])->where($where)->limit($offset,$limit)->order('create_time','desc')->select();
        foreach ($list as $key=>$val){
            $val['user']->hidden(['password', 'salt']);
            $list[$key]['fillTime'] = date("Y-m-d H:i",$val['create_time']);
        }
        $this->success('',$list);
    }

    public function fillDetail()
    {
        $id = $this->request->param('id');
        $fillDetailModel = new Filldetail();
        $list = $fillDetailModel->with(['good'])->where('fill_id',$id)->select();
        $sku_count = $fillDetailModel->where('fill_id',$id)->count('id');
        $num = $fillDetailModel->where('fill_id',$id)->sum('num');

        foreach ($list as $key=>$val){
            $list[$key]['good']['cover'] = cdnurl($val['good']['cover'],true);
        }

        $ret = array();
        $ret['list'] = $list;
        $ret['sku'] = $sku_count;
        $ret['total'] = $num;

        $this->success('',$ret);

    }


    /**
     * 缺货详情
     */
    public function lackDetail()
    {

        $user = $this->auth->getUser();
        $device_id = $this->request->param('device_id');
        $deviceModel = new \app\admin\model\Staffdevice();
        $device = $deviceModel->where('device_id',$device_id)->find();
        $isOk = -1;

        if(!$device){
            $deviceModel = new \app\admin\model\Device();
            $device = $deviceModel->where('device_id',$device_id)->find();
            if($device['agent_id'] == $user['id']){
                $isOk = 1;
            }

        }else{
            if($device['agent_id'] == $user['id']){
                $isOk = 1;
            }
            if($device['user_id'] == $user['id']){
                $isOk = 1;
            }
        }

        if($isOk == -1) $this->error("无权操作次设备");

        $deviceModel = new \app\admin\model\Device();
        $device = $deviceModel->where('device_id',$device_id)->find();


        $slotModel = new Deviceslot();
        $list = $slotModel->with(['good'])->where('device_id',$device_id)->order('slot','asc')->select();
        $lack = 0;
        foreach ($list as $key=>$val){

            $list[$key]['good']['cover'] = cdnurl($val['good']['cover'],true);
            $lack += $val['capacity']-$val['remain'];
        }
        $ret = array();
        $ret['device_name'] = $device['device_name'];
        $ret['address'] = $device['address'];
        $ret['lack'] = $lack;

        $ret['list'] = $list;
        $this->success('',$ret);
    }

    public function doFillDevice()
    {
        $device_id = $this->request->param('device_id');
        $slotList = $this->request->param('slotList');
        $slotList = json_decode(htmlspecialchars_decode($slotList), true);

        $user = $this->auth->getUser();
        $deviceModel = new \app\admin\model\Staffdevice();
        $device = $deviceModel->where('device_id',$device_id)->find();
        if(!$device) $this->error("设备不存在");
        $isOk = -1;
        if($device['agent_id'] == $user['id']){
            $isOk = 1;
        }
        if($device['user_id'] == $user['id']){
            $isOk = 1;
        }
        if($isOk == -1) $this->error("无权操作次设备");

        $total = 0;
        foreach ($slotList as $key=>$val){
            $total += $val['fillCount'];

        }
        $fillModel = new Devicefill();
        $id = $fillModel->insertGetId(['num'=>$total,'device_id'=>$device_id,'user_id'=>$user['id'],'create_time'=>time(),'name'=>date("mdH")."补货"]);

        $fillDetailModel = new Filldetail();
        $deviceSlotModel = new Deviceslot();
        foreach ($slotList as $key=>$val){

            $db = array();
            $db['fill_id'] = $id;
            $db['num'] = $val['fillCount'];
            $db['before_num'] = $val['before'];
            $db['create_time'] = time();
            $db['good_id'] = $val['skuId'];
            $db['slot'] = $val['slotNo'];

            $deviceSlotModel->where('device_id',$device_id)->where('slot',$val['slotNo'])->setInc('remain',$val['fillCount']);
            $fillDetailModel->insert($db);

        }
        $this->success('',$slotList);

    }


    public function staffFillList()
    {

        $user = $this->auth->getUser();
        $deviceModel = new \app\admin\model\Staffdevice();
        $list = $deviceModel->with(['device','slots'])->where('user_id',$user['id'])->select();
        $newList = array();
        foreach ($list as $key=>$val){
            $slotList = $val['slots'];
            $total = 0;
            $remian = 0;
            $emptyCount = 0;
            $lackCount = 0;
            foreach ($slotList as $k=>$v){
                $total+= $v['capacity'];
                $remian += $v['remain'];
                if($v['remain'] == 0){
                    $emptyCount += 1;
                }
            }
            $lackCount  = $total-$remian;

            $percent = 100-$remian/$total*100;
            $percent = intval($percent);
            $val['lack'] = $percent;
            $val['emptyCount'] = $emptyCount;
            $val['lackCount'] = $lackCount;


            if($percent >60){
                $newList[] = $val;
            }
        }
        $this->success('',$newList);
    }
}
